package com.syj.collect.model;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.syj.collect.Present.TaskActivityPresent;
import com.syj.collect.adapter.MyTaskAdapter;
import com.syj.collect.app.R;
import com.syj.collect.base.BaseMvpAvtivity;
import com.syj.collect.base.EmptyRecyclerView;
import com.syj.collect.entry.TaskBean;
import com.syj.collect.view.TaskActivityView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyTaskActivity extends BaseMvpAvtivity<TaskActivityView, TaskActivityPresent> implements TaskActivityView {
    @BindView(R.id.com_rerec)
    EmptyRecyclerView taskRcl;
    @BindView(R.id.comm_refresh)
    SmartRefreshLayout refresh;
    private MyTaskAdapter myTaskAdapter;

    @Override
    public TaskActivityPresent initPresenter() {
        return new TaskActivityPresent();
    }

    int page = 1, type = 1;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        ButterKnife.bind(this);
        initTitle("我的任务");
        try {
            type = getIntent().getIntExtra("ttype", 1);
        } catch (Exception o) {
        }

        initview();

    }

    private void initview() {
        myTaskAdapter = new MyTaskAdapter(this);
        taskRcl.setLayoutManager(new LinearLayoutManager(this));
        taskRcl.setEmptyView(findViewById(R.id.empty_view_));
        taskRcl.setAdapter(myTaskAdapter);
        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.getTaskList1(type);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

            }
        });
        myTaskAdapter.setOnItemCliklister(new MyTaskAdapter.OnItemCliklister() {

            @Override
            public void onClick(TaskBean.DataBean.ListBean dataBean) {
                Intent intent = new Intent(MyTaskActivity.this, MyTaskDetailsActivity.class);
                intent.putExtra("data", dataBean);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initSuccess(int page, List<TaskBean.DataBean.ListBean> str) {
        //if(page==1)
        myTaskAdapter.upDate(str);
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }

    @Override
    public void initFail(String str) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }
}
